package org.droid.cmplatform.module;

/**
 * 
 * @author Karen [Email:keyu404@gmail.com]
 * 
 */
public abstract class AbsTask implements Comparable<AbsTask>, Runnable {

    private TaskLevel taskLevel = null;
    private TaskStatus status = null;

    /**
     * Task priority.
     */
    public enum TaskLevel {
        LEVEL_TASK_LOW,
        LEVEL_TASK_NORMAL,
        LEVEL_TASK_HIGH
    }

    protected AbsTask() {
        this.taskLevel = TaskLevel.LEVEL_TASK_NORMAL;
    }

    protected AbsTask(TaskLevel level) {
        if (level != TaskLevel.LEVEL_TASK_HIGH || level != TaskLevel.LEVEL_TASK_LOW
                || level != TaskLevel.LEVEL_TASK_NORMAL) {
            throw new IllegalArgumentException("Invalid TaskLevel");
        }
        this.taskLevel = level;
    }

    public TaskLevel getTaskLevel() {
        return taskLevel;
    }

    public void setTaskLevel(TaskLevel taskLevel) {
        this.taskLevel = taskLevel;
    }

    /**
     * @return Set the current task status.
     */
    public void setTaskStatus(TaskStatus status) {
        this.status = status;
    }

    /**
     * @return The task current status.
     */
    public TaskStatus getTaskStatus() {
        return status;
    }

    @Override
    public int compareTo(AbsTask another) {
        if (null == another) {
            throw new NullPointerException("AbsTask must be an instance.");
        }

        TaskLevel otherTaskLevel = another.getTaskLevel();
        return this.taskLevel.ordinal() > otherTaskLevel.ordinal() ? 1
                                                                  : (this.taskLevel.ordinal() == otherTaskLevel.ordinal() ? 0
                                                                                                                         : -1);
    }

    @Override
    public void run() {
        this.onProcess();
    }

    /**
     * Task is starting.Note, you must do a few things here.
     */
    public abstract void onStart();

    /**
     * Prepare the task.
     */
    public abstract void onPrepare();

    /**
     * Task is running.
     */
    public abstract void onProcess();

    /**
     * Report the task status.
     */
    public abstract void onProgress();

    /**
     * Task is cancled.
     */
    public abstract void onCancle();

    /**
     * Task is timeout.
     */
    public abstract void onTimeOut();

    /**
     * Result the task.
     */
    public abstract void onResult();

    /**
     * Task is finish.
     */
    public abstract void onFinish();

    /**
     * Task的状态
     * 
     * @author Karen
     * 
     */
    public static enum TaskStatus {
        /**
         * task will starting
         */
        STARTING,
        /**
         * task will runing
         */
        PENDING,
        /**
         * task is running
         */
        RUNING,
        /**
         * task is cancled
         */
        CANCLED,
        /**
         * task is timeout
         */
        TIMEOUT,
        /**
         * task is finish
         */
        FINISHED
    }

}
